package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NotificationActivity extends AppCompatActivity implements View.OnClickListener {

    private Button showBtn, goneBtn;
    private NotificationManager mManager;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        initUI();
    }

    private void initUI() {
        showBtn = findViewById(R.id.showNotBtn);
        goneBtn = findViewById(R.id.goneNotBtn);
        showBtn.setOnClickListener(this);
        goneBtn.setOnClickListener(this);
        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("clark", "测试通知",
                    NotificationManager.IMPORTANCE_LOW);
            mManager.createNotificationChannel(channel);
        }
        Intent intent = new Intent(this, TestActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new NotificationCompat.Builder(this, "clark")
                .setContentTitle("官方通知")
                .setContentText("世界那么大，想去走走吗？")
                .setSmallIcon(R.drawable.ic_baseline_alt_route_24)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.demo1))
                .setColor(Color.parseColor("#ff0000"))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.showNotBtn:
                mManager.notify(1, notification);
                break;
            case R.id.goneNotBtn:
                mManager.cancel(1);
                break;
        }
    }
}